﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNASequencing.core.data
{
    abstract class ADataTree
    {
        /// <summary>
        /// Root node for a data tree.
        /// </summary>
        protected Node _nRoot;

        /// <summary>
        /// All oligonucleotides used in tree.
        /// </summary>
        protected List<AOligonucleotide> _lsAll;

        /// <summary>
        /// All oligonucleotides used in tree.
        /// </summary>
        public List<AOligonucleotide> all
        {
            get
            {
                return _lsAll;
            }
        }

        /// <summary>
        /// Creates new DataTree object with the root node 
        /// in <code>_nRoot</code>
        /// </summary>
        /// <param name="inputData">List of strings, where every string is a sequence of aminoacids.</param>
        /// <param name="complex">Determines if we are constructing the complex or non-complex tree, ie.
        /// if we want every oligonucleotide to hold their followers.</param>
        /// <param name="isForward">Determines if we want to construct forward or backward tree.</param>
        //abstract public DataTree(List<String> inputData/*, bool complex, bool isForward*/);
        //{
        //    _nRoot = new Node();

        //    if (!complex)
        //    {
        //        foreach (String sequence in inputData)
        //        {
        //            Oligonucleotide temp = new Oligonucleotide(sequence);
        //            if (!isForward)
        //            {
        //                BackwardOligonucleotide temp2 = new BackwardOligonucleotide(temp);
        //                _nRoot.add(temp2);
        //            }
        //            else
        //                _nRoot.add(temp);
        //        }
        //    }
        //    else
        //    {
        //        List<Oligonucleotide> all = new List<Oligonucleotide>();

        //        foreach (String sequence in inputData)
        //        {
        //            Oligonucleotide temp = new Oligonucleotide(sequence);
        //            all.Add(temp);
        //        }

        //        foreach (Oligonucleotide o in all)
        //        {
        //            o.updateNext(all);
        //            if (!isForward)
        //            {
        //                BackwardOligonucleotide temp = new BackwardOligonucleotide(o);
        //                _nRoot.add(temp);
        //            }
        //            else
        //                _nRoot.add(o);
        //        }
        //    }
        //}

        /// <summary>
        /// Finds every oligonucleotide that starts with the <code>fragment</code> subsequence.
        /// </summary>
        /// <param name="fragment">Subsequence of the wanted oligonucleotide.</param>
        /// <returns>All sequences that match the <code>fragment</code> pattern.</returns>
        public List<AOligonucleotide> this[String fragment]
        {
            get
            {
                Node curr = _nRoot;
                try
                {
                    for (int i = 0; i < fragment.Length; ++i)
                    {
                        curr = curr[fragment[i]];
                    }
                }
                catch (NullReferenceException)
                {
                    return null;
                }

                return curr.oligonucleotides;
            }
        }
    }
}
